<?php

/**
 * section actions.
 *
 * @package    ems1
 * @subpackage section
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class sectionActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
  }

  public function executeShow(sfWebRequest $request)
  {
    $section = Doctrine::getTable('EmsSection')->find(array($request->getParameter('id')));
    $this->forward404Unless($section);

    return $this->renderPartial('multiUpdateShow', array('section' => $section));
  }

  public function executeNewSubsection(sfWebRequest $request)
  {
      $parent_section = $this->getRoute()->getObject();

      $subsection = new EmsSection();

      $subsection->setPortal($parent_section->getPortal());
      $subsection->setParent($parent_section);
      $subsection->setType($parent_section->getType());
      $subsection->setIsSubsection(true);
      $subsection->setHasSubsection(false);

      $form = new EmsSectionForm($subsection);
      
     return $this->renderPartial('sectionForm'.$subsection->getType()->getType(),
                    array('form' => $form, 'section' => $parent_section)
                );
    
  }

  public function executeCreateSubsection(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $parent_section = $this->getRoute()->getObject();

    $subsection = new EmsSection();

    $subsection->setPortal($parent_section->getPortal());
    $subsection->setParent($parent_section);
    $subsection->setType($parent_section->getType());
    $subsection->setIsSubsection(true);
    $section->setHasSubsection(false);

    $form = new EmsSectionForm($subsection);

    if(!$this->processForm($request, $form)){
        return $this->renderPartial('sectionForm'.$subsection->getType()->getType(),
                    array('form' => $form, 'section' => $parent_section)
                );
    } else {
        return sfView::NONE;
    }
  }

  public function executeNewSection(sfWebRequest $request)
  {
      $portal = $this->getRoute()->getObject();

      $this->forward404Unless($section_type = Doctrine::getTable('EmsSectionType')->findOneByType($request->getParameter('type')));

      $section = new EmsSection();
      $section->setPortal($portal);
      $section->setType($section_type);
      $section->setIsSubsection(false);
      $section->setHasSubsection(false);

      $form = new EmsSectionForm($section);

      return $this->renderPartial('sectionForm'.$section->getType()->getType(),
                    array ('form' => $form, 'portal' => $portal, 'type' => $section_type->getId())
                );
  }

  public function executeCreateSection(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $portal = $this->getRoute()->getObject();

    $this->forward404Unless($section_type = Doctrine::getTable('EmsSectionType')->find(array($request->getParameter('type'))));

    $section = new EmsSection();
    $section->setPortal($portal);
    $section->setType($section_type);
    $section->setIsSubsection(false);
    $section->setHasSubsection(false);

    $form = new EmsSectionForm($section);

    if(!$this->processForm($request, $form)){
        return $this->renderPartial('sectionForm'.$section->getType()->getType(),
                    array ('form' => $form, 'portal' => $portal, 'type' => $section_type->getId())
                );
    } else {
        return sfView::NONE;
    }
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless(
            $section = Doctrine::getTable('EmsSection')->find(array($request->getParameter('id'))),
            sprintf('Object ems_section does not exist (%s).', $request->getParameter('id'))
            );
    
    if($section->getIsSubsection()){
        $form = new EmsSectionForm($section);
        return $this->renderPartial('sectionForm'.$section->getType()->getType(),
                    array('form' => $form, 'section' => $section)
                );
    } else {
        $form = new EmsSectionForm($section);
        return $this->renderPartial('sectionForm'.$section->getType()->getType(),
                    array ('form' => $form, 'portal' => $section->getPortal())
                );
    }
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless(
            $section = Doctrine::getTable('EmsSection')->find(array($request->getParameter('id'))),
            sprintf('Object ems_section does not exist (%s).', $request->getParameter('id'))
            );

    $form = new EmsSectionForm($section);

    if(!$this->processForm($request, $form))
    {
        if($section->getIsSubsection()){
            return $this->renderPartial('sectionForm'.$section->getType()->getType(),
                        array('form' => $form, 'section' => $section)
                    );
        } else {
            return $this->renderPartial('sectionForm'.$section->getType()->getType(),
                        array ('form' => $form, 'portal' => $section->getPortal())
                    );
        }
    } else {
        return sfView::NONE;
    }
  }

//  public function executeDelete(sfWebRequest $request)
//  {
//    $request->checkCSRFProtection();
//
//    $this->forward404Unless($ems_section = Doctrine::getTable('EmsSection')->find(array($request->getParameter('id'))), sprintf('Object ems_section does not exist (%s).', $request->getParameter('id')));
//    $ems_section->delete();
//
//    $this->redirect('section/index');
//  }

    protected function processForm(sfWebRequest $request, sfForm $form)
    {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));

        $valid = $form->isValid();

        if ($valid)
        {
            $audit = new EmsAudit();

            if($form->getObject()->isNew()){
                $audit->setAction(Doctrine::getTable('sfGuardPermission')->findOneByName('Crear sección'));
            } else {
                $audit->setAction(Doctrine::getTable('sfGuardPermission')->findOneByName('Editar sección'));
            }

            $section = $form->save();

            $user = $this->getUser()->getGuardUser();

            $audit->setPortal($section->getPortal());
            $audit->setUser($user);
            $audit->setObjectId($section->getId());

            $audit->save();

            if($section->getIsSubsection() === true){

                $parent = $section->getParent();
                
                if(!$parent->getHasSubsection()){
                    $parent->setHasSubsection(true);
                    $parent->save();
                }
            }
            return $this->renderPartial('multiUpdateSection', array('section' => $section));
        }
        return $valid;
    }
}
